package cn.mxh.pethspt.service.impl;



import cn.mxh.pethspt.dao.JiuZhenDao;
import cn.mxh.pethspt.dao.impl.JiuZhenDaoImpl;
import cn.mxh.pethspt.entity.Ding;
import cn.mxh.pethspt.entity.DingTotal;
import cn.mxh.pethspt.entity.JiuZhen;
import cn.mxh.pethspt.service.JiuZhenService;
import cn.mxh.pethspt.utils.DruidUtils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class JiuZhenServiceImpl implements JiuZhenService {
    JiuZhenDao jiuZhenDao = new JiuZhenDaoImpl();

    /**
     * 添加就诊信息
     *
     * @param jiuZhen
     * @return
     */
    @Override
    public boolean add(JiuZhen jiuZhen) throws Exception {
        Connection connection = null;
        int num = 0;
        try{
            connection = DruidUtils.getConnection();
            connection.setAutoCommit(false);// 关闭事物控制
            num = jiuZhenDao.add(jiuZhen);
            if(num == 1){// 新增订单总表成功
                connection.commit();// 提交
            }
        } catch (Exception e){
            connection.rollback();// 不提交
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return (num == 1);
    }

    /**
     * 修改就诊信息
     *
     * @param jiuzhen
     * @return
     */
    @Override
    public boolean update(JiuZhen jiuzhen) throws Exception {
        Connection connection = null;
        int num = 0;
        try{
            connection = DruidUtils.getConnection();
            connection.setAutoCommit(false);// 关闭事物控制
            num = jiuZhenDao.update(jiuzhen);
            if(num == 1){// 新增订单总表成功
                connection.commit();// 提交
            }
        } catch (Exception e){
            connection.rollback();// 不提交
            e.printStackTrace();
        } finally {
            DruidUtils.close();
        }
        return (num == 1);
    }

    /**
     * 查询所有就诊信息
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<JiuZhen> getAll() throws Exception {
        Connection conn = null;
        List<JiuZhen> jiuZhenList = new ArrayList<>();
        try{
            conn = DruidUtils.getConnection();
            jiuZhenList = jiuZhenDao.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DruidUtils.close(conn);
        }
        return  jiuZhenList;
    }

    /**
     * 根据就诊id查询主人id
     *
     * @param jid
     * @return
     * @throws Exception
     */
    @Override
    public JiuZhen getJiuZhenByjid(Integer jid) throws Exception {
        Connection conn = null;
        JiuZhen jiuZhen = null;
        try{
            conn = DruidUtils.getConnection();
            jiuZhen = jiuZhenDao.getJiuZhenByjid(jid);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DruidUtils.close(conn);
        }
        return jiuZhen;
    }
}
